package com.sskj.award.ui.activity;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.award.R;
import com.sskj.award.R2;
import com.sskj.award.bean.DividendRecordBean;
import com.sskj.award.presenter.DividendRecordPrestener;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.NumberUtil;

import java.util.List;

import butterknife.BindView;

/**
 * 分红记录
 */
@Route(path = RConfig.SUPER_DIVIDEND_RECORD)
public class DividendRecordActivity extends BaseActivity<DividendRecordPrestener> {

    @BindView(R2.id.coolRefreshView)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.tvLeft)
    TextView tvLeft;
    @BindView(R2.id.viewSplit1)
    View viewSplit1;
    @BindView(R2.id.viewSplit2)
    View viewSplit2;
    @BindView(R2.id.tvMoney)
    TextView tvMoney;
    @BindView(R2.id.tvRecordRight)
    TextView tvRecordRight;
    private BaseAdapter<DividendRecordBean.DataBean> dataAdapter;
    @Autowired
    String account;
    @Autowired
    String type;

    private String page="1";
    private boolean state=true;
    @Override
    protected int getLayoutId() {
        return R.layout.super_layout_activity_dividend_record;
    }

    @Override
    public DividendRecordPrestener getPresenter() {
        return new DividendRecordPrestener();
    }

    @Override
    protected void initView() {
        super.initView();
        ARouter.getInstance().inject(this);
        //获取记录
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                if (state){
                    mPresenter.getRecordData(account,page);
                }else {
                    mPresenter.getMoneyRecordData(account,page);
                }
            }
        });
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                if (state){
                    mPresenter.getRecordData(account,page);
                }else {
                    mPresenter.getMoneyRecordData(account,page);
                }
            }
        });
        if (type.equals("001")) {
            tvLeft.setTextColor(getResources().getColor(R.color.super_8e9));
            viewSplit1.setVisibility(View.INVISIBLE);
            tvRecordRight.setTextColor(getResources().getColor(R.color.super_text_blue));
            viewSplit2.setVisibility(View.VISIBLE);
            tvMoney.setText(App.INSTANCE.getString(R.string.super_fenhong_money));
            mPresenter.getMoneyRecordData(account,page);
        }else {
            mPresenter.getRecordData(account,page);
        }

        setTitle(App.INSTANCE.getString(R.string.super_record));
        ClickUtil.click(300, tvRecordRight, () -> {
            tvLeft.setTextColor(getResources().getColor(R.color.super_8e9));
            viewSplit1.setVisibility(View.INVISIBLE);
            tvRecordRight.setTextColor(getResources().getColor(R.color.super_text_blue));
            viewSplit2.setVisibility(View.VISIBLE);
            tvMoney.setText(App.INSTANCE.getString(R.string.super_fenhong_money));
            state=false;
            mPresenter.getMoneyRecordData(account,page);
        });

        ClickUtil.click(300, tvLeft, () -> {
            tvLeft.setTextColor(getResources().getColor(R.color.super_text_blue));
            viewSplit1.setVisibility(View.VISIBLE);
            tvRecordRight.setTextColor(getResources().getColor(R.color.super_8e9));
            viewSplit2.setVisibility(View.INVISIBLE);
            tvMoney.setText(App.INSTANCE.getString(R.string.super_canyu_money));
            state=true;
            mPresenter.getRecordData(account,page);
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setLeftPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 30))
                .setRightPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 30))
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 1))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.super_divi_split))
        );
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initEvent() {
    }

    public void getRecordDataSuccess(DividendRecordBean data) {
        if (data == null) {
            return;
        }
        dataAdapter = new BaseAdapter<DividendRecordBean.DataBean>(R.layout.super_dividend_record_item, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, DividendRecordBean.DataBean item) {
                holder.setText(R.id.tvShowMoney, NumberUtil.keepNoZero(NumberUtil.keep(item.getMoney(),6))+"LAC");
                holder.setText(R.id.tvShowTime, item.getCreated_at());
            }
        };
        dataAdapter.addData(data.getData());
    }

    public void getMoneyRecordDataSuccess(DividendRecordBean body) {

        if (body == null) {
            return;
        }
        List<DividendRecordBean.DataBean> data = body.getData();
        dataAdapter = new BaseAdapter<DividendRecordBean.DataBean>(R.layout.super_dividend_record_item, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, DividendRecordBean.DataBean item) {
                holder.setText(R.id.tvShowMoney, NumberUtil.keepNoZero(NumberUtil.keep(item.getMoney(),6))+item.getCode());
                holder.setText(R.id.tvShowTime, item.getCreated_at());
            }
        };
        dataAdapter.addData(data);
    }

    public void onStopSuccess() {
        if (smartRefreshLayout!=null){
            smartRefreshLayout.finishRefresh();
            smartRefreshLayout.finishLoadMore();
        }
    }
}
